Package cpsc2150.extendedConnectX.models
Interface IGameBoard
- All Known Implementing Classes:
AbsGameBoard,GameBoard,GameBoardMem
public interface IGameBoard
Interface that contains methods for implementing an entire game board for the ExtendedConnectX game.
This interface includes methods to place tokens, check token locations, and determine whether or not the game has been won or is over.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intstatic final intstatic final intstatic final intstatic final intstatic final int -
Method Summary
Modifier and TypeMethodDescriptiondefault booleancheckDiagWin(BoardPosition pos, char p) This function checks to see if the last token placed (which was placed in position pos by player p) resulted in 5 in a row diagonally.default booleancheckForWin(int c) This function will check to see if the last token placed in column c resulted in the player winning the game.default booleancheckHorizWin(BoardPosition pos, char p) This function checks to see if the last token placed (which was placed in position pos by player p) resulted in 5 in a row horizontally.default booleancheckIfFree(int c) Function that returns true if the column can accept another token; false otherwise.default booleancheckTie()This function will check to see if the game has resulted in a tie.default booleancheckVertWin(BoardPosition pos, char p) This function checks to see if the last token placed (which was placed in position pos by player p) resulted in 5 in a row vertically.intReturns the number of columns in the GameBoard.intReturns the number of rows in the GameBoard.intReturns the number of tokens in a row needed to win the game.default booleanisPlayerAtPos(BoardPosition pos, char player) This function returns true if the player is at pos; otherwise, it returns false.voidplaceToken(char p, int c) Function that places the character p in column c.charwhatsAtPos(BoardPosition pos) Function that returns what is in the GameBoard at position pos.
-
Field Details
-
MIN_ROWS
static final int MIN_ROWS- See Also:
-
MAX_ROWS
static final int MAX_ROWS- See Also:
-
MIN_COLUMNS
static final int MIN_COLUMNS- See Also:
-
MAX_COLUMNS
static final int MAX_COLUMNS- See Also:
-
MIN_NUM_TO_WIN
static final int MIN_NUM_TO_WIN- See Also:
-
MAX_NUM_TO_WIN
static final int MAX_NUM_TO_WIN- See Also:
-
-
Method Details
-
checkIfFree
default boolean checkIfFree(int c) Function that returns true if the column can accept another token; false otherwise.- Parameters:
c- Column to be checked- Returns:
- Whether the column can accept another token
- Pre:
- 0
<=c<number_of_columns - Post:
- checkIfFree = true iff ([ #self at column c and row number_of_rows - 1 ] = 'X' OR [ #self at column c and row number_of_rows - 1 ] = 'O') AND self = #self checkIfFree = false iff ([ #self at column c and row number_of_rows - 1 ] = ' ') AND self = #self
-
placeToken
void placeToken(char p, int c) Function that places the character p in column c. The token will be placed in the lowest available row in column c.- Parameters:
p- Character to be placed (representing player token)c- Column to place token in- Pre:
- (p = 'X' OR p = 'O') AND 0
<=c<number_of_columns - Post:
- [ #self at column c and lowest available row ] = 'X' OR [ #self at column c and lowest available row ] = 'O' AND [ token is placed at the lowest available row ] AND [ self = #self plus the new token ]
-
checkForWin
default boolean checkForWin(int c) This function will check to see if the last token placed in column c resulted in the player winning the game. If so it will return true, otherwise false.- Parameters:
c- Column number to be checked- Returns:
- Whether placing a token results in a player winning the game
- Pre:
- 0
<=c<number_of_columns AND [ c is the column where the latest token was placed ] - Post:
- checkForWin = true iff (checkTie = false AND (checkHorizWin = true OR checkVertWin = true OR checkDiagWin = true) WHERE [ c is the column with the last placed token ] AND self = #self checkForWin = false iff (checkTie = true OR (checkHorizWin = false AND checkVertWin = false AND checkDiagWin = false) WHERE [ c is the column with the last placed token ] AND self = #self
-
checkTie
default boolean checkTie()This function will check to see if the game has resulted in a tie. A game is tied if there are no free board positions remaining. It will return true if the game is tied and false otherwise.- Returns:
- Whether the game has resulted in a tie
- Pre:
- [ the game has not ended with a win yet ]
- Post:
- checkTie = true iff ([all board positions are not ' ']) AND self = #self checkTie = false iff ([not all board positions are not ' ']) AND self = #self
-
checkHorizWin
This function checks to see if the last token placed (which was placed in position pos by player p) resulted in 5 in a row horizontally. Returns true if it does, otherwise false.- Parameters:
pos- BoardPosition pair of row and columnp- Player to check with- Returns:
- Whether the last token placed resulted in 5 in a row horizontally
- Pre:
- (0
>=pos.row<number_of_rows AND 0>=pos.column<number_of_columns) AND p != ' ' AND [ pos is the game board position on the latest play ] AND [ p is the token located at position pos ] - Post:
- checkHorizWin = true iff ([num_to_win horizontal positions are p]) AND self = #self checkHorizWin = true iff ([num_to_win horizontal positions are not p]) AND self = #self
-
checkVertWin
This function checks to see if the last token placed (which was placed in position pos by player p) resulted in 5 in a row vertically. Returns true if it does, otherwise false.- Parameters:
pos- BoardPosition pair of row and columnp- Player to check with- Returns:
- Whether the last token placed resulted in 5 in a row vertically
- Pre:
- (0
>=pos.row<number_of_rows AND 0>=pos.column<number_of_columns) AND p != ' ' AND [ pos is the game board position on the latest play ] AND [ p is the token located at position pos ] - Post:
- checkVertWin = true iff ([num_to_win vertical positions are p]) AND self = #self checkVertWin = true iff ([num_to_win vertical positions are not p]) AND self = #self
-
checkDiagWin
This function checks to see if the last token placed (which was placed in position pos by player p) resulted in 5 in a row diagonally. Returns true if it does, otherwise false.- Parameters:
pos- BoardPosition pair of row and columnp- Player to check with- Returns:
- Whether the last token placed resulted in 5 in a row diagonally
- Pre:
- (0
>=pos.row<number_of_rows AND 0>=pos.column<number_of_columns) AND p != ' ' AND [ pos is the game board position on the latest play ] AND [ p is the token located at position pos ] - Post:
- checkVertWin = true iff ([num_to_win diagonal positions are p]) AND self = #self checkVertWin = true iff ([num_to_win diagonal positions are not p]) AND self = #self
-
whatsAtPos
Function that returns what is in the GameBoard at position pos. If no marker is there, it returns a blank space char.- Parameters:
pos- BoardPosition pair of row and column- Returns:
- Character representing player token or space
- Pre:
- (0
>=pos.row<number_of_rows AND 0>=pos.column<number_of_columns) - Post:
- whatsAtPos = [ board at row pos.row and column pos.col ] AND self = #self
-
isPlayerAtPos
This function returns true if the player is at pos; otherwise, it returns false.- Parameters:
pos- BoardPosition pair of row and columnplayer- Player to check with- Returns:
- Whether a player's token is at a position
- Pre:
- (0
>=pos.row<number_of_rows AND 0>=pos.column<number_of_columns) AND player != ' ' - Post:
- isPlayerAtPos = true iff ([ board at row pos.row and column pos.col ] = player) AND self = #self isPlayerAtPos = false iff ([ board at row pos.row and column pos.col ] != player) AND self = #self
-
getNumRows
int getNumRows()Returns the number of rows in the GameBoard.- Returns:
- The number of rows
- Post:
- getNumRows = number_of_rows AND self = #self
-
getNumColumns
int getNumColumns()Returns the number of columns in the GameBoard.- Returns:
- The number of columns
- Post:
- getNumColumns = number_of_columns AND self = #self
-
getNumToWin
int getNumToWin()Returns the number of tokens in a row needed to win the game.- Returns:
- The number of tokens
- Post:
- getNumToWin = num_to_win AND self = #self
-